package com.daxt.controller.warehouse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.daxt.mapper.warehouse.WarehouseMapper;
import com.daxt.mapper.warehouse.WarehouseShelfMapper;
import com.daxt.model.service.warehouse.vo.Warehouse;
import com.daxt.model.service.warehouse.vo.WarehouseShelf;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.warehouse.WarehouseShelfPostionFileMapper;
import com.daxt.mapper.warehouse.WarehouseShelfPostionMapper;
import com.daxt.model.service.warehouse.param.SortPostion;
import com.daxt.model.service.warehouse.result.FileCount;
import com.daxt.model.service.warehouse.vo.WarehouseShelfPostion;
import com.daxt.model.service.warehouse.vo.WarehouseShelfPostionFile;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "位置")
@RestController
@RequestMapping("/warehouse/postion")
public class WarehouseShelfPostionController {
	
	@Autowired 
	private WarehouseShelfPostionMapper warehouseShelfPostionMapper;
	@Autowired
	private WarehouseShelfPostionFileMapper warehouseShelfPostionFileMapper;

	@ApiOperation(value = "货架位置列表", notes = "") 
	@GetMapping("/list/{id}") 
	public Result<List<WarehouseShelfPostion>> list(@PathVariable("id")String id){
		QueryWrapper<WarehouseShelfPostion> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("sId", id);
		List<WarehouseShelfPostion> list = warehouseShelfPostionMapper.selectList(queryWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, list);
	}
	
	@ApiOperation(value = "货架位置列表Count", notes = "") 
	@GetMapping("/listCount/{id}") 
	public Result<List<WarehouseShelfPostion>> listCount(@PathVariable("id")String id){
		QueryWrapper<WarehouseShelfPostion> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("sId", id);
		List<WarehouseShelfPostion> list = warehouseShelfPostionMapper.selectList(queryWrapper);
		for (int i=0;i<list.size();i++) {
			long idd = list.get(i).getId();
			Map<String,Object> param=new HashMap<String, Object>();
			param.put("pId", idd);
			param.put("fileType", "dossier");
			Integer dossierCount = warehouseShelfPostionFileMapper.selectFileCount(param);
			Map<String,Object> param1=new HashMap<String, Object>();
			param1.put("pId", idd);
			param1.put("fileType", "doc");
			Integer docCount = warehouseShelfPostionFileMapper.selectFileCount(param1);
			FileCount fileCount=new FileCount();
			fileCount.setDossierCount(dossierCount==null?0:dossierCount);
			fileCount.setDocCount(docCount==null?0:docCount);
			list.get(i).setFileCount(fileCount);
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, list);
	}
	
	@ApiOperation(value = "货架位置添加", notes = "") 
	@PostMapping("") 
	public Result<String> save(@RequestBody WarehouseShelfPostion entity){
		warehouseShelfPostionMapper .insert(entity);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	@ApiOperation(value = "货架位置修改", notes = "") 
	@PutMapping("") 
	public Result<String> update(@RequestBody WarehouseShelfPostion entity){
		warehouseShelfPostionMapper.updateById(entity);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	@ApiOperation(value = "排序", notes = "") 
	@PutMapping("/sort") 
	@Transactional(rollbackFor = Exception.class)
	public Result<String> sort(@RequestBody List<SortPostion> entity){
		try {
		if(entity!=null&&entity.size()>0) {
			for (SortPostion sortPostion : entity) {
				WarehouseShelfPostion warehouse=new WarehouseShelfPostion();
				warehouse.setId(sortPostion.getId());
				warehouse.setRowIndex(sortPostion.getRowIndex());
				warehouse.setColIndex(sortPostion.getColIndex());
				warehouseShelfPostionMapper.updateById(warehouse);
			}
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), null, null);
		}
		}catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "操作异常！！！", null);
		}
	}
	
	@ApiOperation(value = "货架位置删除", notes = "") 
	@DeleteMapping("") 
	@Transactional(rollbackFor = Exception.class)
	public Result<List<String>> del(@RequestBody List<String> ids){
		try {
		if(ids!=null&&ids.size()>0) {
			List<String> cIds=new ArrayList<String>();
			for (String string : ids) {
				QueryWrapper<WarehouseShelfPostionFile> queryWrapper=new QueryWrapper<>();
				queryWrapper.eq("pId", string);
				List<WarehouseShelfPostionFile> list = warehouseShelfPostionFileMapper.selectList(queryWrapper);
				if(list!=null&&list.size()>0) {
					cIds.add(string);
				}
			}
			if(cIds.size()>0) {
				return ResultUtil.data(ResultCode.RESULT_ERROR_IDS.getCode(), "", cIds);
			}
			warehouseShelfPostionMapper.deleteBatchIds(ids);
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", null);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "删除至少选择一个货架！！！", null);
		}
		}catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "操作异常！！！", null);
		}
	}


}
